package com.sllp.C71S3TsllpShop.web.back;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sllp.C71S3TsllpShop.bean.Business;
import com.sllp.C71S3TsllpShop.bean.ChatRecord;
import com.sllp.C71S3TsllpShop.bean.User;
import com.sllp.C71S3TsllpShop.biz.impl.UserBizImpl;
import com.sllp.C71S3TsllpShop.util.RedisUtil;

@Controller
public class BackCustomerServiceAction {

	@Resource
	private RedisUtil redisUtil;
	@Resource
	private UserBizImpl ubi;
	
	@GetMapping("back/toCustomerService.do")
	public String toCustomerService(@SessionAttribute(name="backLoginBusiness",required=false) Business business,Model m) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			String jsonString = (String) redisUtil.get("business"+business.getId());
			List<ChatRecord> chatRecords = null;
			if(jsonString !=null) {
				chatRecords=mapper.readValue(jsonString, new TypeReference<List<ChatRecord>>(){});
			}else {
				jsonString = "";
			}
			
			List<User> users = new ArrayList<User>();
			if(chatRecords != null) {
				for(ChatRecord chatRecord : chatRecords) {
					User user = new User();
					user.setId(chatRecord.getUser());
					boolean flag = false;
					for(User user1 : users) {
						if(user1.getId()==user.getId()) {
							flag = true;
							break;
						}
					}
					if(flag == false) {
						user = ubi.selectByPrimaryKey(user.getId());
						users.add(user);
					}
				}
			}
			m.addAttribute("business", business);
			m.addAttribute("chatCustomer", users);
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		
		return "back/customer-service::customerService";
	}
	
	@PostMapping("back/showCustomerChatShadow.do")
	public String showCustomerChatShadow(int id,Model m,
			@SessionAttribute(name="backLoginBusiness",required=false) Business business) {
		try {
			ObjectMapper mapper = new ObjectMapper();
			String jsonString = (String) redisUtil.get("business"+business.getId());
			List<ChatRecord> chatRecords=mapper.readValue(jsonString, new TypeReference<List<ChatRecord>>(){});
			List<ChatRecord> chatRecords2 = new ArrayList<ChatRecord>();
			for(ChatRecord chatRecord : chatRecords) {
				if(chatRecord.getUser()==id) {
					chatRecords2.add(chatRecord);
				}
			}
			User user = ubi.selectByPrimaryKey(id);
			m.addAttribute("business", business);
			m.addAttribute("chatCustomerNow", user); 
			m.addAttribute("chatRecords", chatRecords2);
			System.out.println(id+"==========="+chatRecords2.size()+"=="+user.getTrueName()); 
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
		return "back/customer-service::customerChatShadow";
	}
	
}
